﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace xamlfactor.xamlfactor_lib
{
    public class XmlHelper
    {
        #region Public Static Methods

        public static IEnumerable<Element> GetElementsFromXml(string xml)
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.LoadXml(xml);

            return GetElementsFromNode(xmlDocument.DocumentElement);
        }

        public static IEnumerable<Element> GetElementsFromNode(XmlNode node)
        {
            IEnumerable<Element> elements = new List<Element>
                                                {
                                                    new Element(GetAttributeValuesFromXmlNode(node))
                                                };
            if (node.HasChildNodes)
                elements = elements.Union(GetElementsFromNode(node.FirstChild));

            if (node.NextSibling != null)
                elements = elements.Union(GetElementsFromNode(node.NextSibling));

            return elements;
        }

        public static IEnumerable<AttributeValue> GetAttributeValuesFromXmlNode(XmlNode node)
        {
            return node.Attributes.Cast<XmlAttribute>().Select(x => new AttributeValue(x.Name, x.Value));
        }

        #endregion

    }
}
